#导入pygame模块
import pygame
from pygame.locals import *
import time,random

class Snake:
	'''贪吃蛇类'''
	def __init__(self,screen_temp):
		self.x = 200
		self.y = 200
		self.screen = screen_temp
		self.img1 = pygame.image.load("./images/s_head.png")
		self.img2 = pygame.image.load("./images/s_body.png")
		self.gamevoer = pygame.image.load("./images/s_gamevoer.png")
		self.wall = [{"left":5},
					{"right":512},
					{"up":5},
					{"down":331}] #设置墙的边界
		self.list = [(self.x,self.y),
					(self.x - 10,self.y),
					(self.x - 20,self.y)] #初始化蛇

	def display(self):
		for i in range(len(self.list)):
			self.screen.blit(self.img2,(self.list[i]))

	def move_left(self):
		if self.list[0][0] <= self.list[1][0]:
			self.newhead = (self.list[0][0] - 10,self.list[0][1]) #设置新的头
			self.list.insert(0,self.newhead)
			self.delete()
		if self.list[0][0] < self.wall[0]["left"]:
			self.die()
		self.bumpself()

	def move_right(self):
		if self.list[0][0] >= self.list[1][0]:
			self.newhead = (self.list[0][0] + 10,self.list[0][1])
			self.list.insert(0,self.newhead)
			self.delete()
		if self.list[0][0] > self.wall[1]["right"]:
			self.die()
		self.bumpself()

	def move_up(self):
		if self.list[0][1] <= self.list[1][1]:
			self.newhead = (self.list[0][0],self.list[0][1] - 10)
			self.list.insert(0,self.newhead)
			self.delete()
		if self.list[0][1] < self.wall[2]["up"]:
			self.die()
		self.bumpself()
		
	def move_down(self):
		if self.list[0][1] >= self.list[1][1]:
			self.newhead = (self.list[0][0],self.list[0][1] + 10)
			self.list.insert(0,self.newhead)
			self.delete()
		if self.list[0][1] > self.wall[3]["down"]:
			self.die()
		self.bumpself()	

	#判断是否撞到自己
	def bumpself(self):
		for bum in self.list[1:]:
			if self.list[0] == bum :
				self.die()

	#没有吃到食物，递减最后一个，因为是在不断插入新的头，所以要减掉尾
	def delete(self):
		del self.list[-1]

	#游戏结束函数
	def die(self):
		print("gamevoer")
		self.screen.blit(self.gamevoer,(0,0))
		while True:
			pygame.display.update()
			for event in pygame.event.get():
				if event.type == pygame.QUIT:
					pygame.quit()
					exit()

	def drawTwxt(self):
		'''绘制贪吃蛇长度'''
		self.length = len(self.list)
		pygame.font.init()
		self.font = pygame.font.SysFont('arial',12)
		self.text_surface = self.font.render("length = %d"%self.length,True,(0,0,0),(0,255,127))
		self.textRect = self.text_surface.get_rect()
		self.textRect.center = (480,5)
		return self.text_surface,self.textRect

class Food:
	'''食物类'''
	def __init__(self,screen_temp):
		#随机出现食物
		self.x = random.choice(range(10,500,10))
		self.y = random.choice(range(10,320,10))
		self.screen = screen_temp
		self.img = pygame.image.load("./images/s_food.png")

	def display(self,snake_temp):
		self.screen.blit(self.img,(self.x,self.y))
		#如果蛇吃掉食物，重新设置食物的位置
		if self.x == snake_temp.list[0][0] and self.y == snake_temp.list[0][1] :
			snake_temp.list.append(snake_temp.list[-1])
			self.x = random.choice(range(10,500,10))
			self.y = random.choice(range(10,320,10))		

def key_cntrol(snake_temp):
	'''键盘控制函数'''
	#执行退出操作
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			pygame.quit()
			exit()
	#获取键盘信息
	key_pressed = pygame.key.get_pressed()
	#左移动
	if key_pressed[K_LEFT] or key_pressed[K_a]:
		snake_temp.move_left()
		print("left")
	#右移动
	elif key_pressed[K_RIGHT] or key_pressed[K_d] :
		snake_temp.move_right()
		print("right")
	#上移动
	elif key_pressed[K_UP] or key_pressed[K_w] :
		snake_temp.move_up()
		print("up")
	#下移动
	elif key_pressed[K_DOWN] or key_pressed[K_s] :
		snake_temp.move_down()
		print("down")

#创建游戏主窗口 
def main():
	'''程序主函数'''
	#创建游戏主窗口
	screen = pygame.display.set_mode((517,336),0,0)
	#设置游戏标题
	pygame.display.set_caption("贪吃蛇")
	#设置程序背景
	background = pygame.image.load("./images/snake_bg.jpg")
	#实例化蛇
	snake = Snake(screen)
	#实例化食物
	food = Food(screen)

	while True:
		#绘制游戏背景
		screen.blit(background,(0,0))
		#展示蛇
		snake.display()
		#绘制蛇长度
		text_surface,textRect = snake.drawTwxt()
		screen.blit(text_surface,textRect)
		#绘制食物
		food.display(snake)
		#键盘控制
		key_cntrol(snake)
		#更新显示
		pygame.display.update()
		#定时显示，还能控制输出的键盘信息的次数，如果不加此语句，按一下键盘，这个键会输出好多次
		time.sleep(0.1)

#判断当前是否是主程序，如果是就执行 
if __name__ == "__main__" :
	main()
# 
# 
# 
# 

# 
# 
# 